package com.suredy.app.porject.model.vo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.suredy.security.entity.UserEntity;

/**
 * 收款计划筛选模型
 * 
 * @author VIVID.G
 * @since 2016-2-24
 * @version v0.1
 */
public class PayinPlanFilterModel {

	/* 合同编号 */
	private String contractNo;

	/* 合同名称 */
	private String contractName;

	/* 收款单位 */
	private String planFromCompany;

	/* 收款金额 */
	private Double planMinInMoney;

	/* 收款金额 */
	private Double planMaxInMoney;

	/* 收款占比 */
	private Double planMinInMoneyRatio;

	/* 收款占比 */
	private Double planMaxInMoneyRatio;

	/* 收款日期 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMinInTime;

	/* 收款日期 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date planMaxInTime;

	/* 负责人 */
	private String planHeader;

	/* 计划变更 */
	private Boolean planChanged;

	/* 执行情况 */
	private Boolean planBack;

	public String getContractNo() {
		return contractNo;
	}

	public void setContractNo(String contractNo) {
		this.contractNo = contractNo;
	}

	public String getContractName() {
		return contractName;
	}

	public void setContractName(String contractName) {
		this.contractName = contractName;
	}

	public String getPlanFromCompany() {
		return planFromCompany;
	}

	public void setPlanFromCompany(String planFromCompany) {
		this.planFromCompany = planFromCompany;
	}

	public Double getPlanMinInMoney() {
		return planMinInMoney;
	}

	public void setPlanMinInMoney(Double planMinInMoney) {
		this.planMinInMoney = planMinInMoney;
	}

	public Double getPlanMaxInMoney() {
		return planMaxInMoney;
	}

	public void setPlanMaxInMoney(Double planMaxInMoney) {
		this.planMaxInMoney = planMaxInMoney;
	}

	public Double getPlanMinInMoneyRatio() {
		return planMinInMoneyRatio;
	}

	public void setPlanMinInMoneyRatio(Double planMinInMoneyRatio) {
		this.planMinInMoneyRatio = planMinInMoneyRatio;
	}

	public Double getPlanMaxInMoneyRatio() {
		return planMaxInMoneyRatio;
	}

	public void setPlanMaxInMoneyRatio(Double planMaxInMoneyRatio) {
		this.planMaxInMoneyRatio = planMaxInMoneyRatio;
	}

	public Date getPlanMinInTime() {
		return planMinInTime;
	}

	public void setPlanMinInTime(Date planMinInTime) {
		this.planMinInTime = planMinInTime;
	}

	public Date getPlanMaxInTime() {
		return planMaxInTime;
	}

	public void setPlanMaxInTime(Date planMaxInTime) {
		this.planMaxInTime = planMaxInTime;
	}

	public String getPlanHeader() {
		return planHeader;
	}

	public void setPlanHeader(String planHeader) {
		this.planHeader = planHeader;
	}

	public Boolean getPlanChanged() {
		return planChanged;
	}

	public void setPlanChanged(Boolean planChanged) {
		this.planChanged = planChanged;
	}

	public Boolean getPlanBack() {
		return planBack;
	}

	public void setPlanBack(Boolean planBack) {
		this.planBack = planBack;
	}

	public List<Object> makeQl() {
		String ql = "SELECT T FROM ContractPayinPlan T LEFT JOIN T.contract C WHERE 1=1";

		return this.makeWhereQl(ql, true);
	}

	public List<Object> makeCountQl() {
		String ql = "SELECT COUNT(T) FROM ContractPayinPlan T LEFT JOIN T.contract C WHERE 1=1";

		return this.makeWhereQl(ql, false);
	}

	private List<Object> makeWhereQl(String ql, boolean order) {
		List<Object> ret = new ArrayList<Object>();

		List<Object> params = new ArrayList<Object>();

		// 合同编号
		if (!StringUtils.isBlank(this.contractNo)) {
			ql += " AND C.no like ?";
			params.add("%" + this.contractNo + "%");
		}

		// 合同名称
		if (!StringUtils.isBlank(this.contractName)) {
			ql += " AND C.name like ?";
			params.add("%" + this.contractName + "%");
		}

		// 收款单位
		if (!StringUtils.isBlank(this.planFromCompany)) {
			ql += " AND T.moneyFrom like ?";
			params.add("%" + this.planFromCompany + "%");
		}

		// 收款金额
		if (this.planMinInMoney != null) {
			ql += " AND T.total >= ?";
			params.add(this.planMinInMoney);
		}

		// 收款金额
		if (this.planMaxInMoney != null) {
			ql += " AND T.total <= ?";
			params.add(this.planMaxInMoney);
		}

		// 收款占比
		if (this.planMinInMoneyRatio != null) {
			ql += " AND (T.total >= (C.price * ?))";
			params.add(this.planMinInMoneyRatio / 100);
		}

		// 收款占比
		if (this.planMaxInMoneyRatio != null) {
			ql += " AND (T.total <= (C.price * ?))";
			params.add(this.planMaxInMoneyRatio / 100);
		}

		// 收款日期
		if (this.planMinInTime != null) {
			ql += " AND T.backTime >= ?";
			params.add(this.planMinInTime);
		}

		// 收款日期
		if (this.planMaxInTime != null) {
			ql += " AND T.backTime <= ?";
			params.add(this.planMaxInTime);
		}

		// 负责人
		if (!StringUtils.isBlank(this.planHeader)) {
			ql += " AND T.header = ?";
			params.add(new UserEntity(this.planHeader));
		}

		// 计划变更
		if (this.planChanged != null) {
			// 已变更
			if (this.planChanged)
				ql += " AND T.changed = ?";

			// 未变更
			else
				ql += " AND (T.changed IS NULL OR T.changed = ?)";

			params.add(this.planChanged);
		}

		// 执行情况
		if (this.planBack != null) {
			// 已收款
			if (this.planBack)
				ql += " AND T.back = ?";

			// 未收款
			else
				ql += " AND (T.back IS NULL OR T.back = ?)";

			params.add(this.planBack);
		}

		// 排序
		if (order)
			ql += " ORDER BY T.back, T.backTime";

		ret.add(ql);
		ret.add(params);

		return ret;
	}
}
